﻿using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Web.UI.WebControls;
using AMS.WorkBill.Entities;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Helper;

namespace AMS.Web.UserControls
{
    public partial class FileUpLoadCtl : System.Web.UI.UserControl
    {
       
        public string WoIdVal
        {
            private get { return hdnKeyVal.Value; }
            set { hdnKeyVal.Value = value; }
        }



        protected void Page_Load(object sender, EventArgs e)
        {

            SetData();
        }

        private void SetData()
        {
            if (!IsPostBack)
            {
                if (WoIdVal.Trim().Length == 36)
                {
                    var acc = new BaseDataAccess();
                    IList objList = acc.GetObjectList(new WorkBillInfo().GetType().Name, "Id", WoIdVal);
                    if (objList != null && objList.Count > 0)
                    {
                        var currentWo = (WorkBillInfo)objList[0];
                        string woName = currentWo.Theme;
                        lblWoName.Text = "[ " + woName + " ]工单附件列表:";
                    }
                }
            }
            fileDiv.Controls.Clear();
            if (WoIdVal.Trim().Length == 36)
            {
                string filePath = "~/WorkBill/FileUpLoad/" + WoIdVal;
                if (Directory.Exists(Server.MapPath(filePath)))
                {
                    var sb = new StringBuilder();
                    var dirInfo = new DirectoryInfo(Server.MapPath(filePath));

                    foreach (FileInfo file in dirInfo.GetFiles("*.*"))
                    {
                        var panel = new Panel();
                        var link = new HyperLink
                                       {
                                           NavigateUrl = filePath + "/" + file,
                                           Text = file.Name,
                                           ToolTip = "点击可查看详细"
                                       };
                        link.Attributes.Add("target", "_blank");
                        var btnDel = new LinkButton
                        {
                            Text = "删除",
                            ID = file.Name,
                            OnClientClick = " return confirm('确认删除所选附件吗');"
                        };
                        btnDel.Style.Add("margin-left", "20px");
                        btnDel.Click += new EventHandler(this.btnDel_Click);
                        panel.Controls.Add(link);
                        panel.Controls.Add(btnDel);
                        panel.Style.Add("margin-top", "10px");
                        panel.Style.Add("background", "#f5fbff");
                        fileDiv.Controls.Add(panel);
                    }
                }

            }

        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            var btn = (LinkButton)sender;
            var fileName = btn.ID;
            var file = "~/WorkBill/FileUpLoad/" + WoIdVal + "/" + fileName;
            if (File.Exists(Server.MapPath(file)))
            {
                File.Delete(Server.MapPath(file));
                Alert.MessageBox("删除成功!");
                SetData();
            }
        }

        protected void btnUploadFile_Click(object sender, EventArgs e)
        {
            if (upFile.HasFile)
            {
                if (WoIdVal.Trim().Length == 36)
                {
                    string filePath = "~/WorkBill/FileUpLoad/" + WoIdVal;
                    if (!Directory.Exists(Server.MapPath(filePath)))
                    {
                        Directory.CreateDirectory(Server.MapPath(filePath));
                    }
                    string file = filePath + "/" + upFile.FileName;
                    upFile.SaveAs(MapPath(file));
                    Alert.MessageBox("上传成功！");
                    SetData();
                }
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(Page.ClientScript.GetType(), "7578", "<script>alert('请选择要上传的文件!')</script>");
            }
        }


    }
}